package cn.jly.spring.boot.cloud.dao.impl;

import cn.jly.spring.boot.cloud.beans.Employee;
import cn.jly.spring.boot.cloud.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author lanyangji
 * @description
 * @date 2020/11/24 下午 3:33
 */
@Service
public class EmployeeDaoImpl implements EmployeeDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Employee> getList() {
        return jdbcTemplate.query(
                "select * from tbl_employee",
                new RowMapper<Employee>() {
                    @Override
                    public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
                        final Employee employee = new Employee();
                        employee.setId(rs.getInt("id"));
                        employee.setLastName(rs.getString("last_name"));
                        employee.setEmail(rs.getString("email"));
                        employee.setGender(rs.getString("gender"));

                        return employee;
                    }
                }
        );
    }
}
